/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.listener;

import com.webanovare.model.UserRegistry;
import com.webanovare.model.db.User;
import com.webanovare.model.db.UserGroup;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * This is just to put some data into the database
 *
 * @author hajo
 */
@WebListener()
public class SecurityListener implements ServletContextListener {

    @EJB
    UserRegistry userRegistry;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // Dummy data
        User admin = new User("admin", "admin");
        User normal = new User("user", "user");
        User kalle = new User("kalle", "kalle");
        User apa = new User("apa", "apa");
        kalle.addGroup(UserGroup.USER);
        apa.addGroup(UserGroup.USER);
        admin.addGroup(UserGroup.ADMIN);
        admin.addGroup(UserGroup.USER);
        normal.addGroup(UserGroup.USER);
        
        try {
            // Will get exception if users aready there
            userRegistry.add(admin);
            userRegistry.add(normal);
            userRegistry.add(kalle);
            userRegistry.add(apa);
        } catch (Exception e) {
            Logger.getAnonymousLogger().log(Level.INFO, "Couldn't add admin and user");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

}
